<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2020/8/24
 * Time: 18:23
 */

namespace ticket\libs;


use ticket\libs\Zhiyoubao\Configura;

class CheckSign extends Configura
{

    public function checkSignTicketList($params)
    {
        if (!isset($params['signType']) || empty($params['signType'])){
            return ['code' => 10044,'msg' => '签名有误，signType'];
        }
        $paramsData = '';

        if ($params['signType'] == "MD5"){
            $checkStatus = $this->signTicket($params);
            if ($checkStatus['code'] !== 0){
                return $checkStatus;
            }
            $paramsDataStatus = $this->getMd5Data($params);  // 校验Md5参数
            if ($paramsDataStatus !== 0){
                return $paramsDataStatus;
            }
            $paramsData = $params;
        }elseif($params['signType'] == 'AES'){
            $paramsDataStatus = $this->getAesData($params);// 解密Aes参数
            if ($paramsDataStatus['code'] != 0){
                return $paramsDataStatus;
            }
            $paramsData = $paramsDataStatus['msg'];
            if (!empty($paramsData)){
                $checkStatus = $this->signTicket($paramsData);
                if ($checkStatus['code'] !== 0){
                    return $checkStatus;
                }
            }else{
                return ['code' => 40055,'msg' => '签名数据有误'];
            }
        }
        return ['code' => 0,'msg' => $paramsData];
    }


    /*** 校验门票列表参数
     * @param $params
     * @return array
     */
    public function signTicket($params)
    {

        if (!isset($params['appId']) || empty($params['appId'])){
            return ['code' => 10044,'msg' => '签名有误，appId'];
        }

        if (!isset($params['nonceStr']) || empty($params['nonceStr'])){
            return ['code' => 10044,'msg' => '签名有误，nonceStr'];
        }
        if (!isset($params['timeStamp']) || empty($params['timeStamp'])){
            return ['code' => 10044,'msg' => '签名有误，timeStamp'];
        }
        if (!isset($params['aid']) || empty($params['aid'])){
            return ['code' => 10044,'msg' => '签名有误，aid'];
        }

        $this->getApplication($params['aid']);
        return ['code' => 0,'msg' => 'success'];
    }












    /*** 解密Aes参数
     * @param $params
     * @return array
     */
    private function getAesData($params)
    {
        if (!isset($params['aid']) || empty($params['aid'])){
            return ['code' => 10044,'msg' => '参数有误'];
        }
        $this->getApplication($params['aid']);
        $key = $this->aes_key;
        $iv = $this->aes_iv;
        $paramsData = json_decode($this->decryption($params['data'],$key,$iv),true);
        return ['code' => 0,'msg' => $paramsData];
    }


    /*** 校验Md5参数
     * @param $params
     * @return array
     */
    private function getMd5Data($params)
    {
        if (!isset($params['sign']) || empty($params['sign'])){
            return ['code' => 10044,'msg' => '签名有误，sign'];
        }
        $app_key = $this->app_key;  // 获取应用key
        if (empty($app_key)){
            return ['code' => 40055,'msg' => '签名错误'];   // 40055 应用id有误
        }
        $sign = empty($params['sign'])?'':$params['sign'];
        $postSign = Tools::computeSign($params,$app_key);
//            var_dump($postSign);die;
        if ($sign !== $postSign){
            return ['code' => 10011,'msg' => '签名有误']; // 10011 签名有误
        }

        return ['code' => 0,'msg' => $params];
    }





}